<?php

class SalesManageController extends Controller
{

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(

            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('add','search','getlist','getbyid','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
		);
	}

    public function actionAdd()
    {
        $sales = new Sales();
        if(!empty($_POST['id']))$sales = Sales::model()->findByPk(new MongoId($_POST['id']));
        $query = array(
            'conditions' => array(
                'account'=>array('=='=>$_POST['account']),
                // 'id'=>array('='=>new MongoId($_POST['id'])),
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
        );
        $result = Sales::model()->count($query);
        if($result>0&&(empty($_POST["id"])))
        {
            header("HTTP/1.0 400 account is exist");
            die();
        }

        if($result>0&&!empty($_POST["id"])&&$_POST['account']!=$sales->account)
        {
            header("HTTP/1.0 400 account is exist");
            die();
        }

        $query = array(
            'conditions' => array(
                'email'=>array('=='=>$_POST['email']),
                // 'id'=>array('='=>new MongoId($_POST['id'])),
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
        );
        $result = Sales::model()->count($query);
        if($result>0&&empty($_POST["id"]))
        {
            header("HTTP/1.0 400 email is exist");
            die();
        }

        if($result>0&&!empty($_POST["id"])&&$_POST['email']!=$sales->email)
        {
            header("HTTP/1.0 400 email is exist");
            die();
        }

        $sales->nickname = $_POST["nickname"];
        $sales->brand = $_POST["brand"];
        $sales->shop = $_POST["shop"];
        $sales->telephone = $_POST["telephone"];
        if(empty($_POST['id']))$sales->email = $_POST["email"];
        if(empty($_POST['id']))$sales->account = $_POST["account"];;
        $sales->password = $_POST["password"];;
        $sales->status = 1;
        
        $sales->save(false);
    }

    /**
     * 获得版本信息
     */
    public function actionGetList($iDisplayStart=0,$iDisplayLength=10)
    {
        $resultColumn = array('nickname','shop',);
        $query = array(
            'conditions' => array(
                'status'=>array('>'=>0),
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
            'select'=>$resultColumn,
            'limit'=>$iDisplayLength,
            'offset'=>$iDisplayStart,
        );

        $result = Sales::model()->findAll($query);

      
        $records = array();
        $records["aaData"] = array(); 

        if(!empty($result))
            foreach ($result as $key => $value) {
            $tmp = array();
            $value->_id = (string)$value->_id;
            foreach ($value as $key1=>$value1) {
                if(in_array($key1, $resultColumn))
                {
                    if($key1=='icon')
                        array_push($tmp, "<img src='/dubai/{$value1}' class='img-thumnail' style='max-height:100px' alrt=''>");
                    else
                        array_push($tmp, $value1);
                }
            }
            array_push($tmp, '<a href="javascript:;" data-id="'.$value->_id.'" class="btn btn-xs blue btn-editable"><i class="fa fa-pencil"></i> Edit</a> <a href="javascript:;" data-id="'.$value->_id.'" class="btn btn-xs red btn-removable"><i class="fa fa-times"></i> Delete</a>  ');
            array_push($records["aaData"], $tmp);
        }

        $records["iTotalRecords"] = Sales::model()->count();
        $records["iTotalDisplayRecords"] = Sales::model()->count();
  
        echo json_encode($records);
    }

    public function actionSearch($name)
    {
        $resultColumn = array('name_en','_id');
        $query = array(
            'conditions' => array(
                'status' => array('>'=>0),
                'name_en'=>array('=='=>new MongoRegex("/^{$name}/i")),
                'parentId'=>array('=='=>new MongoRegex("/[1-9]/i")),
                // 'code'=>array('>'=>10),
            ),
            'select'=>$resultColumn,
        );

        $result = Category::model()->findAll($query);
        $records = array();
        if(!empty($result))
        {
            foreach ($result as $key => $value) {
                $value->_id = (string)$value->_id;
                foreach ($value as $key1=>$value1) {
                    if(!in_array($key1, $resultColumn))
                    {
                        unset($value[$key1]);
                    }
                }

                $obj['id'] = $value->_id;
                $obj['text'] = $value->name_en;
                array_push($records, $obj);
            }
        }



        echoStringToMobile(json_encode(array("results"=>$records)));

    }

    public function actionGetById($id)
    {
        $brand = Sales::model()->findByPk(new MongoId($id));
        $brand->_id = (string)$brand->_id;
        $brand = $brand->toArray();
        $brand['brandname'] = Brand::model()->findByPk(new MongoId($brand['brand']))->name_en;
        echoStringToMobile(json_encode($brand));
    }

    public function actionDelete($id)
    {
        $sales = Sales::model()->findByPk(new MongoId($id));
        $sales->status = -1;
        $sales->save(false);
    }
}